package com.ruoyi.device.service.impl;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.device.api.domain.DeviceOnOffLog;
import com.ruoyi.device.api.domain.DeviceOnline;
import com.ruoyi.device.mapper.DeviceOnlineMapper;
import com.ruoyi.device.service.IDeviceOnOffLogService;
import com.ruoyi.device.service.IDeviceOnlineService;
import com.ruoyi.device.service.IDeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 设备在线信息Service业务层处理
 * 
 * @author luogongquan
 * @date 2024-09-06
 */
@Service
public class DeviceOnlineServiceImpl implements IDeviceOnlineService 
{
    @Autowired
    private DeviceOnlineMapper deviceOnlineMapper;
    @Autowired
    private IDeviceService deviceService;

    @Autowired
    private IDeviceOnOffLogService deviceOnOffLogService;

    /**
     * 查询设备在线信息
     * 
     * @param deviceId 设备在线信息主键
     * @return 设备在线信息
     */
    @Override
    public DeviceOnline selectDeviceOnlineByDeviceId(Long deviceId)
    {
        return deviceOnlineMapper.selectDeviceOnlineByDeviceId(deviceId);
    }

    /**
     * 查询设备在线信息列表
     * 
     * @param deviceOnline 设备在线信息
     * @return 设备在线信息
     */
    @Override
    public List<DeviceOnline> selectDeviceOnlineList(DeviceOnline deviceOnline)
    {
        return deviceOnlineMapper.selectDeviceOnlineList(deviceOnline);
    }

    /**
     * 新增设备在线信息
     * 
     * @param deviceOnline 设备在线信息
     * @return 结果
     */
    @Override
    public int insertDeviceOnline(DeviceOnline deviceOnline)
    {
        deviceOnline.setCreateTime(DateUtils.getNowDate());
        return deviceOnlineMapper.insertDeviceOnline(deviceOnline);
    }

    /**
     * 修改设备在线信息
     * 
     * @param deviceOnline 设备在线信息
     * @return 结果
     */
    @Override
    public int updateDeviceOnline(DeviceOnline deviceOnline)
    {
        deviceOnline.setUpdateTime(DateUtils.getNowDate());
        return deviceOnlineMapper.updateDeviceOnline(deviceOnline);
    }

    /**
     * 批量删除设备在线信息
     * 
     * @param deviceIds 需要删除的设备在线信息主键
     * @return 结果
     */
    @Override
    public int deleteDeviceOnlineByDeviceIds(Long[] deviceIds)
    {
        return deviceOnlineMapper.deleteDeviceOnlineByDeviceIds(deviceIds);
    }

    /**
     * 删除设备在线信息信息
     * 
     * @param deviceId 设备在线信息主键
     * @return 结果
     */
    @Override
    public int deleteDeviceOnlineByDeviceId(Long deviceId)
    {
        return deviceOnlineMapper.deleteDeviceOnlineByDeviceId(deviceId);
    }

    @Override
    public Boolean InsertOrUpdateDeviceOnline(DeviceOnline deviceOnline) {
        //新增或更新设备上下线信息
        Long deviceId = deviceService.selectDeviceByNo(deviceOnline.getDeviceNo());
        if(deviceId == null){
            return Boolean.FALSE;
        }
        deviceOnline.setDeviceId(deviceId);
        deviceOnline.setOnlineTime(deviceOnline.getCreateTime());
        deviceOnline.setFirstOnlineTime(deviceOnline.getCreateTime());
        deviceOnlineMapper.InsertOrUpdateDeviceOnline(deviceOnline);
        //插入一条设备上下线记录
        DeviceOnOffLog log = new DeviceOnOffLog();
        log.setDeviceId(deviceId);
        log.setDeviceNo(deviceOnline.getDeviceNo());
        log.setTime(deviceOnline.getCreateTime());
        log.setOnlineType(deviceOnline.getIsOnline());
        deviceOnOffLogService.insertDeviceOnOffLog(log);
        return Boolean.TRUE;
    }

}
